<!--
 * @Author: 王灿
 * @Date: 2019-11-29 09:25:05
 * @LastEditTime: 2019-12-07 00:06:27
 * @Description: 客户编辑
 * @FilePath: \scarlet-sparrow-admin\src\views\home\home-customer\customer-operate\index
-->

<template>
  <div>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm">
      <el-form-item prop="realName">
        <el-row>
          <el-input v-model="ruleForm.realName" placeholder="请输入姓名" clearable></el-input>
        </el-row>
      </el-form-item>
      <el-form-item prop="nick">
        <el-row>
          <el-input v-model="ruleForm.nick" placeholder="请输入昵称" clearable></el-input>
        </el-row>
      </el-form-item>
      <el-form-item prop="mobile">
        <el-row>
          <el-input
            v-model="ruleForm.mobile"
            type="tel"
            maxlength="11"
            placeholder="请输入手机号"
            clearable
          ></el-input>
        </el-row>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="onSubmitForm('ruleForm')">确 定</el-button>
      <el-button @click="onCancelForm('ruleForm')">取 消</el-button>
    </div>
  </div>
</template>
<script>
import { getCustomerUpdate } from '@/api/home.js'
export default {
  name: 'CustomerOperate',
  props: {
    row: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      ruleForm: {
        userId: '',
        realName: '',
        mobile: '',
        nick: ''
      },
      rules: {
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          {
            parrent: /^1[3456789]\d{9}$/,
            message: '手机号格式不正确',
            trigger: 'blur'
          }
        ],
        realName: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        nick: [{ required: true, message: '请输入昵称', trigger: 'blur' }],
      }
    }
  },
  watch: {
    row() {
      this.onGetRow()
    }
  },
  created() {
    this.onGetRow()
  },
  methods: {
    // 默认数据
    onGetRow() {
      let boolean = JSON.stringify(this.row) !== '{}'
      this.ruleForm.userId = boolean ? this.row.userId : ''
      this.ruleForm.realName = boolean ? this.row.realName : ''
      this.ruleForm.mobile = boolean ? this.row.mobile : ''
      this.ruleForm.nick = boolean ? this.row.nick : ''
    },
    // 提交
    onSubmitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          if (this.row.id) {
            // 编辑
            getCustomerUpdate(this.ruleForm).then(res => {
              this.$notify.success({
                title: '提示',
                message: '编辑成功'
              })
              this.onRefresh()
            })
          }
        }
      })
    },
    // 取消
    onCancelForm(formName) {
      this.$refs[formName].resetFields()
      let data = { isShow: false }
      this.$emit('addTableRow', data)
    },
    // 刷新页面
    onRefresh() {
      setTimeout(() => {
        let data = { isShow: false, isRefresh: true }
        this.$emit('addTableRow', data)
      }, 1000)
    }
  }
}
</script>